package com.baidu.disconf.client;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.Ordered;
import org.springframework.core.PriorityOrdered;

import com.baidu.disconf.client.config.DisClientConfig;
import com.baidu.disconf.client.store.aspect.DisconfAspectJ;
import com.baidu.disconf.client.store.inner.DisconfCenterHostFilesStore;
import com.baidu.disconf.client.store.inner.DisconfCenterStore;
import com.baidu.disconf.client.support.utils.StringUtil;
import com.baidu.disconf.client.watch.WatchFactory;
import com.baidu.disconf.core.common.zookeeper.ZookeeperMgr;
import com.gitee.hermer.engine.jars.app.sdk.EngineAppClassLoader;

/**
 * 绗竴娆℃壂鎻忥紝闈欐�佹壂鎻�
 *
 * @author liaoqiqi
 * @version 2014-6-17
 */
public class DisconfMgrBean implements BeanDefinitionRegistryPostProcessor, PriorityOrdered, ApplicationContextAware {

	public final static String SCAN_SPLIT_TOKEN = ",";

	private ApplicationContext applicationContext;

	private String scanPackage = null;

	public void destroy() {
		DisconfMgr.getInstance().close();
		if(Thread.currentThread().getContextClassLoader() instanceof EngineAppClassLoader){
			WatchFactory.release();
			DisClientConfig.release();
			DisconfCenterStore.release();
		}

	}

	public void setScanPackage(String scanPackage) {
		this.scanPackage = scanPackage;
	}

	@Override
	public int getOrder() {
		return Ordered.HIGHEST_PRECEDENCE + 1;
	}

	/**
	 * 杩欎釜鍑芥暟鏃犳硶杈惧埌鏈�楂樹紭鍏堢骇锛屼緥濡侾ropertyPlaceholderConfigurer
	 */
	@Override
	public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
	}

	/**
	 * 绗竴娆℃壂鎻�<br/>
	 * 鍦⊿pring鍐呴儴鐨凚ean瀹氫箟鍒濆鍖栧悗鎵ц锛岃繖鏍锋槸鏈�楂樹紭鍏堢骇鐨�
	 */
	@Override
	public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {

		// 涓轰簡鍋氬吋瀹�
		DisconfCenterHostFilesStore.getInstance().addJustHostFileSet(fileList);

		List<String> scanPackList = StringUtil.parseStringToStringList(scanPackage, SCAN_SPLIT_TOKEN);
		// unique
		Set<String> hs = new HashSet<String>();
		hs.addAll(scanPackList);
		scanPackList.clear();
		scanPackList.addAll(hs);

		// 杩涜鎵弿
		DisconfMgr.getInstance().setApplicationContext(applicationContext);
		DisconfMgr.getInstance().firstScan(scanPackList);

		// register java bean
		registerAspect(registry);
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		this.applicationContext = applicationContext;
	}

	/**
	 * register aspectJ for disconf get request
	 *
	 * @param registry
	 */
	private void registerAspect(BeanDefinitionRegistry registry) {

		GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
		beanDefinition.setBeanClass(DisconfAspectJ.class);
		beanDefinition.setLazyInit(false);
		beanDefinition.setAbstract(false);
		beanDefinition.setAutowireCandidate(true);
		beanDefinition.setScope("singleton");

		registry.registerBeanDefinition("disconfAspectJ", beanDefinition);
	}

	/*
	 * 宸茬粡搴熷純浜嗭紝涓嶆帹鑽愪娇鐢�
	 */
	@Deprecated
	private Set<String> fileList = new HashSet<String>();

	@Deprecated
	public Set<String> getFileList() {
		return fileList;
	}

	@Deprecated
	public void setFileList(Set<String> fileList) {
		this.fileList = fileList;
	}
}
